{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "39d85cb1",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "1. 购物车项目详解\n",
    "2. 字典一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78c008cc",
   "metadata": {},
   "source": [
    "要求：    1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "　　   　 2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "　　  　　 3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "       \n",
    "　　　　  4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "00102a75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的余额：1\n",
      "_________【商品列表】__________\n",
      "商品编号：0,商品名称：珍珠奶茶小杯,商品价格：7元\n",
      "商品编号：1,商品名称：珍珠奶茶中杯,商品价格：9元\n",
      "商品编号：2,商品名称：珍珠奶茶大杯,商品价格：11元\n",
      "商品编号：3,商品名称：珍珠奶茶1L,商品价格：15元\n",
      "请输入正确的商品编号（输入q可退出购买）：q\n",
      "您的购物车：[] \n",
      " 您的账户余额：1 元\n"
     ]
    }
   ],
   "source": [
    "# 商品列表\n",
    "\n",
    "goods_list = [\n",
    "    #商品名称 价格 编号 等信息\n",
    "    #模板: [商品名称，商品名称]\n",
    "    [\"珍珠奶茶小杯\",7],\n",
    "    [\"珍珠奶茶中杯\",9],\n",
    "    [\"珍珠奶茶大杯\",11],\n",
    "    [\"珍珠奶茶1L\",15],\n",
    "]\n",
    "user_shopping_cart = []\n",
    "# 用户输入\n",
    "Alipay = int(input(\"请输入您的余额：\"))\n",
    "\n",
    "# 打印商品列表 （规范打印，样式是先写的，然后再输入数据）\n",
    "# 先设计模板样式：商品编号：xx  商品名称：xx  商品价格：xx元\n",
    "\n",
    "while True:\n",
    "    print(\"_________【商品列表】__________\")\n",
    "    for i in range(len(goods_list)):\n",
    "    # print(\"商品编号：%s,商品名称：%s,商品价格：%d元\"%(0,goods_list[0][0],goods_list[0][1]))\n",
    "        print(\"商品编号：%s,商品名称：%s,商品价格：%d元\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "    # 2.让用户输入商品编号进行商品购买：\n",
    "    # 用户输入商品编号\n",
    "    choice = input(\"请输入正确的商品编号（输入q可退出购买）：\") # 用户输入可能不是数值，也可能是\n",
    "\n",
    "    if choice.isdigit(): \n",
    "        # isdigit 判断字符串是否是数值\n",
    "        choice = int(choice) \n",
    "        # 用int转换数据类型为数值\n",
    "        # 是正确的编号\n",
    "        if choice < len(goods_list): #商品的信息：goods_list[choice] , 商品名称：goods_list[choice][0] , 商品价格：goods_list[choice][1]\n",
    "            # 1.看一看用户钱够不够： alipay vs goods_list[choice][1]\n",
    "            if Alipay < goods_list[choice][1]:\n",
    "                print(\"您的余额不够，请更换支付方式\")\n",
    "                # 买得起 1. 扣钱，2. 把商品加到用户购物列表\n",
    "                Alipay = Alipay - goods_list[choice][1]\n",
    "                user_shopping_cart.append(goods_list[choice])\n",
    "                print(\"【%s】已经加入您的购物车，您的【余额】还有 %s 元\"%(goods_list[choice][0],Alipay))\n",
    "            # 不是正确的编号    \n",
    "            else:\n",
    "                print(\"您的余额不足以购买，请更换支付方式\")\n",
    "                break\n",
    "    # 两种情况\n",
    "    # 1. 按照我们要求输入的文本，例如输入“q”表示退出\n",
    "    # 2. 输入不符合我们要求的文本\n",
    "    elif choice == \"q\":\n",
    "        # 如果用户输入q 则说明用户要退出购买了，我们将打印用户已购买的商品列表 以及 用户余额\n",
    "        print(\"您的购物车：%s\"%(user_shopping_cart),'\\n',\"您的账户余额：%s 元\"%(Alipay))\n",
    "        break\n",
    "    else:\n",
    "        print(\"请输入正确的【商品编码】！！或者按照输入提示进行输入\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
